package com.four.peoplemanage.leave.service;

import com.four.peoplemanage.common.bean.Page;
import com.four.peoplemanage.leave.dao.LeaveDao;
import com.four.peoplemanage.leave.entity.Leave;
import com.four.peoplemanage.overtime.entity.Overtime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class LeaveService {
    @Autowired
    LeaveDao leaveDao;
    //申请请假
    public boolean insert(Leave leave){
        if (leave == null){
            return false;
        }
        leave.setLeave_status("待审核");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataTimeString = sdf.format(new Date());
        leave.setHolidayendtime(dataTimeString);
        leave.setHolidaybegintime(dataTimeString);
        return leaveDao.addLeave(leave)>0;
    }

    //分页
    public Page<Leave> getPageLeave (int pageNum, int pageSize, String name, String leave_type, int information_id,String leave_status){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Leave> list=leaveDao.getLeave(start,pageSize,name,leave_type,information_id,leave_status);
        //获取总条目数
        int total=leaveDao.countLeave(name,leave_type,leave_status);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Leave> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
    //分页
    public Page<Leave> getAllLeave (int pageNum, int pageSize, String name, String leave_type, String leave_status){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Leave> list=leaveDao.getAllLeave(start,pageSize,name,leave_type,leave_status);
        //获取总条目数
        int total=leaveDao.countLeave(name,leave_type,leave_status);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Leave> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    //撤销
    public  boolean updateStatus(Leave leave){
        if (leave==null){
            return false;
        }
        leave.setLeave_status("已撤销");
        return leaveDao.updateLeave(leave)>0;
    }

    //审核通过
    public  boolean updateSuccess(Leave leave){
        if (leave==null){
            return false;
        }
        leave.setLeave_status("已通过");
        return leaveDao.updateLeave(leave)>0;
    }

    //审核驳回
    public  boolean updateFail(Leave leave){
        if (leave==null){
            return false;
        }
        leave.setLeave_status("已驳回");
        return leaveDao.updateLeave(leave)>0;
    }

    //编辑
    public boolean updateLeave(Leave leave){
        if (leave == null){
            return false;
        }
        leave.setLeave_status("待审核");
        return leaveDao.updateLeaveById(leave)>0;
    }
}
